/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wargame.controller;

import java.sql.Timestamp;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import wargame.dao.*;
import wargame.entity.*;
import module.*;
import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Clarence
 */
@Controller
@RequestMapping(value = "/register")
public class Register {

    @RequestMapping(value = "/registerPlayer.do", method = RequestMethod.POST)
    public ModelAndView registerPlayer(@RequestParam(value = "player_name", required = true) String player_name,
            @RequestParam(value = "company_name", required = true) String company_name,
            HttpServletRequest request, HttpServletResponse response, Model model) {

        // if you want to return an object into the index.jsp page,
        // create a ${variablename} in jsp page
        // add model - addAttribute("variablename", object)

        String error = "";
        ModelAndView mv = new ModelAndView();

        // Check if both fields are entered
        if (player_name.equals("") || company_name.equals("")) { // Blank fields detected
            error = "Player name or company name is empty!";
        } else {
            // Check if Company has been taken
            if (PlayerDAO.retrievePlayerByCompanyName(company_name) != null) { // Company Name is taken
                error = "Company name has been taken, please choose another name!";
            } else {

                Player p = new Player();
                p.setPlayer_name(player_name);
                p.setCompany_name(company_name);
                p.setBalance(GlobalConstant.PLAYER_STARTING_AMOUNT);
                int new_player_id = PlayerDAO.insertPlayer(p);
                // Player Added into Database
                
                // Create Player Session
                PlayerSession ps = new PlayerSession();
                ps.setPlayer_id(new_player_id);

                // insert player into session
                HttpSession session = request.getSession(true);
                session.setAttribute(GlobalConstant.PLAYER_SESSION, ps);
                mv.addObject("announcement", "You have successfully created a company!");

                Notification notification = new Notification();
                notification.setMessage("Welcome to MIC Tycoon! Manage you employee and project to be a successfull IT company!");
                notification.setPlayer_id(new_player_id);
                notification.setTurn(0);
                NotificationDAO.insertNotification(notification);

                // Adding Game Log
                GameLog gl = new GameLog();
                gl.setPlayer_id(new_player_id);
                gl.setTurn(GameTurn.current_turn);
                gl.setDescription("Successfully Registered a new company");
                gl.setTimestamp(new Timestamp(new Date().getTime()));

                GameLogDAO.insertGameLog(gl);

                mv.setViewName("redirect:../servlet/player/dashboard");
                
                return mv;
            }
        }
        // Confirm error, just throw out as display message.
        mv.addObject("error_message", error);
        mv.addObject("player_name", player_name);
        mv.addObject("company_name", company_name);
        mv.setViewName("register");
        return mv;

    }
}
